package ph.com.gs3.formalistics.model.valueobjects.business;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

public class MessageThread implements Serializable {

	private static final long serialVersionUID = 1859028693137744154L;

	private String id;
	private int unreadMessageCount;
	private List<String> subscriberIdList;
	private List<String> recipientIdList;
	private String latestMessage;
	private String latestDatePosted;
	private String latestMessageAuthorId;
	private String title;
	private String imageURL;

	public static MessageThread createFromJSON(JSONObject json) throws JSONException {

		MessageThread thread = new MessageThread();

		// @formatter:off
		thread.id 						= json.getString("thread_id");
		thread.unreadMessageCount 		= json.getInt("unread_messages");
		thread.latestMessage 			= json.getString("latest_message");
		thread.latestMessageAuthorId 	= json.getString("latest_message_author_id");
		thread.latestDatePosted 		= json.getString("latest_message_date_posted");
		thread.title 					= json.getString("title");
		thread.imageURL 				= json.getString("image_url");
		
		String[] subscriberListArray 	= json.getString("subscribers").split(",");
		String[] recipientListArray 	= json.getString("recipients").split(",");

		thread.subscriberIdList 		= new ArrayList<>(Arrays.asList(subscriberListArray));
		thread.recipientIdList 			= new ArrayList<>(Arrays.asList(recipientListArray));
		// @formatter:on		

		return thread;

	}

	// =====================================================================
	// {{ Getters & Setters

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getUnreadMessageCount() {
		return unreadMessageCount;
	}

	public void setUnreadMessageCount(int unreadMessageCount) {
		this.unreadMessageCount = unreadMessageCount;
	}

	public List<String> getSubscriberIdList() {
		return subscriberIdList;
	}

	public void setSubscriberIdList(List<String> subscriberIdList) {
		this.subscriberIdList = subscriberIdList;
	}

	public List<String> getRecipientIdList() {
		return recipientIdList;
	}

	public void setRecipientIdList(List<String> recipientIdList) {
		this.recipientIdList = recipientIdList;
	}

	public String getLatestMessage() {
		return latestMessage;
	}

	public void setLatestMessage(String latestMessage) {
		this.latestMessage = latestMessage;
	}

	public String getLatestDatePosted() {
		return latestDatePosted;
	}

	public void setLatestDatePosted(String latestDatePosted) {
		this.latestDatePosted = latestDatePosted;
	}

	public String getLatestMessageAuthorId() {
		return latestMessageAuthorId;
	}

	public void setLatestMessageAuthorId(String latestMessageAuthorId) {
		this.latestMessageAuthorId = latestMessageAuthorId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getImageURL() {
		return imageURL;
	}

	public void setImageURL(String imageURL) {
		this.imageURL = imageURL;
	}

	// }}

}
